package com.jy.rhin.infra.repo.notfication.subscribe.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jy.rhin.infra.repo.notfication.subscribe.po.SubscribePO;
import com.jy.rhin.infra.repo.notfication.subscribe.query.dto.SubscribeFullDto;
import com.jy.rhin.sdk.dto.notification.resp.SubscribeInfoDto;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author WeiQ
 * @date 2022/6/28 14:03
 */
@Repository
public interface SubscribeMapper extends BaseMapper<SubscribePO> {

    /**
     * 分页查询主题订阅列表
     *
     * @param page
     * @param queryWrapper
     * @return
     */
    @Select(value = "select a.id,                                   \n" +
            " a.subscribe_uid as subscribeUid,                      \n" +
            " a.subscriber_code as subscriberCode,                  \n" +
            " a.topic_exp as topicExp,                              \n" +
            " a.status,                                             \n" +
            " b.topic_name as topicName,                            \n" +
            " b.publisher_code as publisherCode,                    \n" +
            " c.participant_name as publisherName,                  \n" +
            " d.participant_name as subscriberName,                 \n" +
            " d.interaction_type as interactionType,                \n" +
            " a.create_time as createTime                           \n" +
            " from t_notification_subscribe a                       \n" +
            " left join t_notification_topic b on a.topic_exp = b.topic_exp \n" +
            " left join t_notification_participant c on b.publisher_code = c.participant_code and c.participant_role = 'PUBLISHER'  \n" +
            " left join t_notification_participant d on a.subscriber_code = d.participant_code  and d.participant_role = 'SUBSCRIBER'  \n" +
            " ${ew.customSqlSegment} ")
    Page<SubscribeInfoDto> queryPage(Page<Object> page, @Param(Constants.WRAPPER) QueryWrapper<Object> queryWrapper);

    @Select(value = "select count(*) \n" +
            " from t_notification_subscribe a                       \n" +
            " left join t_notification_topic b on a.topic_exp = b.topic_exp \n" +
            " left join t_notification_participant c on b.publisher_code = c.participant_code and c.participant_role = 'PUBLISHER'  \n" +
            " left join t_notification_participant d on a.subscriber_code = d.participant_code  and d.participant_role = 'SUBSCRIBER'  \n")
    Integer getSubscriberCount();

    /**
     * 查询主题订阅详情
     *
     * @param subscribeId
     * @return
     */
    @Select("select a.id,                                                                                       \n " +
            "       a.subscribe_uid                                            as subscribeUid,                 \n " +
            "       a.subscriber_code                                          as subscriberCode,               \n " +
            "       a.topic_exp                                                as topicExp,                     \n " +
            "       a.query_exp                                                as queryExp,                     \n " +
            "       d.query_filter_exp                                         as filterExp,                    \n " +
            "       b.participant_name                                         as subscriberName,               \n " +
            "       b.interaction_type                                         as interactionType,               \n " +
            "       c.topic_name                                               as topicName,                    \n " +
            "       e.query_name                                               as queryName,                    \n " +
            "       f.filter_name                                              as filterName,                   \n " +
            "       f.target_path                                              as targetPath,                     \n " +
            "       f.filter_type                                              as filterType,                     \n " +
            "       f.filter_code                                              as filterCode,                     \n " +
            "       d.query_filter_value                                       as value                         \n " +
            "from t_notification_subscribe a                                                                    \n " +
            "         join t_notification_participant b                                                         \n " +
            "              on b.participant_role = 'SUBSCRIBER' and b.participant_code = a.subscriber_code      \n " +
            "         join t_notification_topic c on c.topic_exp = a.topic_exp                                  \n " +
            "         left join t_notification_subscribe_filter d on a.id = d.subscribe_id                      \n " +
            "         left join t_notification_topic_query e on e.topic_exp = a.topic_exp and e.query_exp = a.query_exp  \n " +
            "         left join t_notification_topic_query_filter f                                             \n " +
            " on f.topic_exp = a.topic_exp and f.query_exp = a.query_exp and f.filter_exp = d.query_filter_exp \n " +
            "where a.id = #{subscribeId}                                                                        \n ")
    List<SubscribeFullDto> queryDetail(@Param("subscribeId") Long subscribeId);
}
